package com.ruoyi.web.controller.system;

import java.util.ArrayList;
import java.util.List;
import java.util.Optional;

import com.ruoyi.system.domain.Equipment;
import com.ruoyi.system.domain.vo.EquipmentMaintenanceRecordVo;
import com.ruoyi.system.service.IEmployeeService;
import com.ruoyi.system.service.IEquipmentService;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.context.annotation.Bean;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.system.domain.EquipmentMaintenanceRecord;
import com.ruoyi.system.service.IEquipmentMaintenanceRecordService;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.utils.poi.ExcelUtil;
import com.ruoyi.common.core.page.TableDataInfo;

import javax.annotation.Resource;

/**
 * 设备维护信息Controller
 * 
 * @author 管鸿影
 * @date 2024-12-08
 */
@Controller
@RequestMapping("/system/record")
public class EquipmentMaintenanceRecordController extends BaseController
{
    private String prefix = "system/record";

    @Resource
    private IEquipmentService equipmentService;

    @Autowired
    private IEquipmentMaintenanceRecordService equipmentMaintenanceRecordService;


    @GetMapping()
    public String record()
    {
        return prefix + "/record";
    }

    /**
     * 查询设备维护信息列表
     */

    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(EquipmentMaintenanceRecord equipmentMaintenanceRecord)
    {
        startPage();
        List<EquipmentMaintenanceRecord> list = equipmentMaintenanceRecordService.selectEquipmentMaintenanceRecordList(equipmentMaintenanceRecord);
        List<EquipmentMaintenanceRecordVo> equipmentMaintenanceRecordVos = new ArrayList<>();
        list.forEach(er -> {
            Equipment equipment = equipmentService.selectEquipmentByEquipmentId(er.getEquipmentId());
            EquipmentMaintenanceRecordVo equipmentMaintenanceRecordVo = new EquipmentMaintenanceRecordVo();
            BeanUtils.copyProperties(er,equipmentMaintenanceRecordVo);
            equipmentMaintenanceRecordVo.setEquipment(Optional.ofNullable(equipment).map(Equipment::getEquipmentName).orElse("--"));
            equipmentMaintenanceRecordVos.add(equipmentMaintenanceRecordVo);
        });
        return getDataTable(equipmentMaintenanceRecordVos);
    }

    /**
     * 导出设备维护信息列表
     */
    @RequiresPermissions("system:record:export")
    @Log(title = "设备维护信息", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(EquipmentMaintenanceRecord equipmentMaintenanceRecord)
    {
        List<EquipmentMaintenanceRecord> list = equipmentMaintenanceRecordService.selectEquipmentMaintenanceRecordList(equipmentMaintenanceRecord);
        ExcelUtil<EquipmentMaintenanceRecord> util = new ExcelUtil<EquipmentMaintenanceRecord>(EquipmentMaintenanceRecord.class);
        return util.exportExcel(list, "设备维护信息数据");
    }

    /**
     * 新增设备维护信息
     */
    @GetMapping("/add")
    public String add(ModelMap modelMap)
    {
        List<Equipment> equipments = equipmentService.selectEquipmentList(new Equipment());
        modelMap.put("equipments",equipments);
        return prefix + "/add";
    }

    /**
     * 新增保存设备维护信息
     */
    @RequiresPermissions("system:record:add")
    @Log(title = "设备维护信息", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(EquipmentMaintenanceRecord equipmentMaintenanceRecord)
    {
        return toAjax(equipmentMaintenanceRecordService.insertEquipmentMaintenanceRecord(equipmentMaintenanceRecord));
    }

    /**
     * 修改设备维护信息
     */
    @RequiresPermissions("system:record:edit")
    @GetMapping("/edit/{maintenanceId}")
    public String edit(@PathVariable("maintenanceId") Long maintenanceId, ModelMap mmap)
    {
        EquipmentMaintenanceRecord equipmentMaintenanceRecord = equipmentMaintenanceRecordService.selectEquipmentMaintenanceRecordByMaintenanceId(maintenanceId);
        mmap.put("equipmentMaintenanceRecord", equipmentMaintenanceRecord);
        List<Equipment> equipments = equipmentService.selectEquipmentList(new Equipment());
        mmap.put("equipments",equipments);
        return prefix + "/edit";
    }

    /**
     * 修改保存设备维护信息
     */
    @RequiresPermissions("system:record:edit")
    @Log(title = "设备维护信息", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(EquipmentMaintenanceRecord equipmentMaintenanceRecord)
    {
        return toAjax(equipmentMaintenanceRecordService.updateEquipmentMaintenanceRecord(equipmentMaintenanceRecord));
    }

    /**
     * 删除设备维护信息
     */
    @RequiresPermissions("system:record:remove")
    @Log(title = "设备维护信息", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        return toAjax(equipmentMaintenanceRecordService.deleteEquipmentMaintenanceRecordByMaintenanceIds(ids));
    }
}
